/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package batailledesardennes.game.map;

import batailledesardennes.game.GameEnv;
import batailledesardennes.util.geomFloat.Point;
import batailledesardennes.util.geomFloat.path.Node;
import batailledesardennes.util.geomFloat.path.Obstacle;
import java.util.ArrayList;
import java.util.List;
import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.geom.Line;
import org.newdawn.slick.geom.Polygon;

/**
 *
 * @author charly
 */
public abstract class GameMap {
   protected MapAccessHandler accessHandler;
   protected int width;
   protected int height;
   protected GameEnv env;

   public GameMap() {
      accessHandler = new MapAccessHandler();
   }

   public void init(GameEnv env) {
      this.env = env;
      env.setMap(this);
   }

   public abstract void draw(Graphics g);

   public MapAccessHandler getAccessHandler() {
      return accessHandler;
   }

   public int getHeight() {
      return height;
   }

   public int getWidth() {
      return width;
   }

   public void setEnv(GameEnv env) {
      this.env = env;
   }

   protected void drawObstacles(Graphics g) {
      g.setColor(Color.blue);
      List<Obstacle> obstacles = accessHandler.getObstacles();
      for (Obstacle obstacle : obstacles) {
         // draw obstacle
         List<Float> pts = new ArrayList<Float>();
         for (Point p : obstacle.getPolygon().getPoints()) {
            pts.add(p.getX());
            pts.add(p.getY());
         }
         float[] ptsArray = new float[pts.size()];
         for (int i = 0; i < pts.size(); i++) {
            ptsArray[i] = pts.get(i);
         }
         Polygon polygon = new Polygon(ptsArray);
         g.setColor(Color.pink);
         g.draw(polygon);
         // draw connections
         g.setColor(Color.blue);
         for (Node node : obstacle.getNodes()) {
            for (Node node2 : node.getConnectedNodes()) {
               Point quarterPt = node.getPoint().midPoint(node.getPoint().midPoint(node2.getPoint()));
               g.setLineWidth(3f);
               Line line = new Line(node.getPoint().getX(), node.getPoint().getY(),
                                    quarterPt.getX(), quarterPt.getY());
               g.draw(line);
               g.setLineWidth(1f);
               line = new Line(quarterPt.getX(), quarterPt.getY(),
                               node2.getPoint().getX(), node2.getPoint().getY());
               g.draw(line);
            }

         }
      }
   }
}
